package com.example.list.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据库sql语句 并 执行
        String sql = "create table " + Constants.TABLE_NAME + "(name varchar(20))";
//        String sql = "create table " + Constants.TABLE_NAME + "(name varchar(20),age integer)";
//        String sql = "create table " + Constants.TABLE_NAME + "(name varchar(20),age integer,sex varchar)";
        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


        String sql;

        switch (oldVersion) {
            case 1:

                break;

            case 2:
                Log.e(Constants.TAG, "onUpgrade: "+Constants.VERSION_CODE);
                sql = "alter table " + Constants.TABLE_NAME + " add age integer";
                db.execSQL(sql);
                break;
            case 3:
                Log.e(Constants.TAG, "onUpgrade: "+Constants.VERSION_CODE);
                sql = "alter table " + Constants.TABLE_NAME + " add age integer";
                String sql2 = "alter table " + Constants.TABLE_NAME + " add sex varchar";
                db.execSQL(sql);
                db.execSQL(sql2);
                break;
            case 4:
                break;

        }
    }
}
